#!/bin/bash

# Hysteria Rust Uninstallation Script

set -e

echo "Uninstalling Hysteria Rust..."

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "Please run as root (use sudo)"
    exit 1
fi

# Stop services
echo "Stopping services..."
systemctl stop hysteria 2>/dev/null || true
systemctl stop hysteria-admin 2>/dev/null || true

# Disable services
echo "Disabling services..."
systemctl disable hysteria 2>/dev/null || true
systemctl disable hysteria-admin 2>/dev/null || true

# Remove systemd service files
echo "Removing systemd service files..."
rm -f /etc/systemd/system/hysteria.service
rm -f /etc/systemd/system/hysteria-admin.service

# Reload systemd
systemctl daemon-reload

# Remove binary
echo "Removing binary..."
rm -f /usr/local/bin/hysteria

# Ask about configuration and data
read -p "Remove configuration files? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    rm -rf /etc/hysteria
fi

read -p "Remove data files? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    rm -rf /var/lib/hysteria
fi

read -p "Remove log files? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    rm -rf /var/log/hysteria
fi

read -p "Remove hysteria user? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    userdel hysteria 2>/dev/null || true
fi

echo ""
echo "Uninstallation completed!"
